import java.io.*;
import java.util.StringTokenizer;

public class 采药 {
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    public static void main1(String[] args) throws IOException {
        int m = in.nextInt();
        int n = in.nextInt();
        // 前 i 个草药中 j 分钟的最大价值
        int[][] dp = new int[n + 1][m + 1];
        for (int i = 1,a,b; i <= n; i++) {
            a = in.nextInt();
            b = in.nextInt();
            for (int j = 1; j <= m; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j >= a) {
                    dp[i][j] = Math.max(dp[i][j],dp[i - 1][j - a] + b);
                }
            }
        }
        out.println(dp[n][m]);
        out.close();
    }

    public static void main(String[] args) throws IOException {
        int m = in.nextInt();
        int n = in.nextInt();
        // 前 i 个草药中 j 分钟的最大价值
        int[] dp = new int[m + 1];
        for (int i = 1,a,b; i <= n; i++) {
            a = in.nextInt();
            b = in.nextInt();
            for (int j = m; j >= a; j--) {
                dp[j] = dp[j];
                dp[j] = Math.max(dp[j],dp[j - a] + b);
            }
        }
        out.println(dp[m]);
        out.close();
    }











    public static class Read {
        StringTokenizer st = new StringTokenizer("");
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String next() throws IOException {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(bf.readLine());
            }
            return st.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
    }
}
